import 'package:flutter/material.dart';
import 'package:xbaistack/common/gen.dart';
import 'package:xbaistack/model/user.dart';
import 'package:xbaistack/service/user_service.dart';
import 'package:xbaistack/widget/refresh_list.dart';

class RefreshDemoPage extends StatefulWidget {
  static const String routeName = "RefreshDemoPage";
  RefreshDemoPage()
    : super(key: GlobalKey<_RefreshDemoPageState>(debugLabel: 'RefreshPage'));

  void toRefresh() {
    (super.key as GlobalKey<_RefreshDemoPageState>).currentState?.refresh();
  }

  @override
  State<StatefulWidget> createState() => _RefreshDemoPageState();
}

class _RefreshDemoPageState extends State<RefreshDemoPage> {
  late final service = UserService();
  late final RefreshController<User> _controller;

  @override
  void initState() {
    super.initState();
    _controller = RefreshController(
      future: (page) {
        return service.selectPageList(page: page, size: 12, query: {});
      },
    );
  }

  void refresh() {
    _controller.refresh();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("列表封装")),
      body: RefreshList(
        controller: _controller,
        builder: (context, index, users, user) {
          return Container(
            color: Colors.white,
            padding: const EdgeInsets.all(10),
            child: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.start,
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                Padding(
                  padding: const EdgeInsets.only(bottom: 5),
                  child: Text("${user.nickname}（${user.username}）"),
                ),
                Text(
                  "邮箱：${user.email}",
                  style: const TextStyle(color: Colors.black45),
                ),
                Text(
                  "住址：${user.address}",
                  style: const TextStyle(color: Colors.black45),
                ),
              ],
            ),
          );
        },
      ),
    );
  }
}
